package loveqq.niceexcel.converter.internal.localdatetime;

import loveqq.niceexcel.annotation.internal.Internal;
import loveqq.niceexcel.converter.CellData;
import loveqq.niceexcel.converter.ConvertContext;
import loveqq.niceexcel.converter.Converter;
import loveqq.niceexcel.util.DateUtils;
import org.apache.poi.ss.usermodel.CellType;

import java.time.LocalDateTime;

/**
 * @author zhongjh@tsintergy.com
 * @date 2022-08-18
 */
@Internal
public class LocalDateTimeStringConverter implements Converter<LocalDateTime> {
    @Override
    public Class<LocalDateTime> supportJavaType() {
        return LocalDateTime.class;
    }

    @Override
    public CellType supportExcelType() {
        return CellType.STRING;
    }

    @Override
    public LocalDateTime convertToJavaData(CellData<?> cellData, ConvertContext context) {
        String value = cellData.getStringValue();
        return (value == null) ? null : DateUtils.parseLocalDateTime(value, context.getDateTimeFormat());
    }

    @Override
    public CellData<?> convertToExcelData(LocalDateTime data, ConvertContext context) {
        String value = (data == null) ? null : DateUtils.format(data, context.getDateTimeFormat());
        return new CellData<>(value);
    }
}
